package cn.yyh.serviceOrg.controller;


import cn.yyh.commons.utils.PageInfo;
import cn.yyh.commons.utils.R;
import cn.yyh.serviceOrg.entity.SystemDictionaryType;
import cn.yyh.serviceOrg.query.DictionaryTypeQuery;
import cn.yyh.serviceOrg.service.SystemDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yang
 * @since 2021-07-10
 */
@RestController
@RequestMapping("/serviceOrg/type")
public class DictionaryTypeController {
    @Autowired
    private SystemDictionaryTypeService dictionaryType;

    /**
     * 查询所有数据字典分类
     * @version: 1.0
     * @return: R
     * @author: 杨永航
     * @date: 2021/6/28
     */
    @GetMapping
    public R listAllType(){
        try {
            List<SystemDictionaryType> listAll = dictionaryType.listAll();
            return R.oK().data("rows", listAll);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @PostMapping("saveType")
    public R saveOrUpdateType(@RequestBody SystemDictionaryType type){
        try {
            if (type.getId() == null) {
                dictionaryType.save(type);
            }else {
                dictionaryType.update(type);
            }
            return R.oK();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @DeleteMapping("{id}")
    public R deleteType(@PathVariable Long id) {
        try {
            dictionaryType.delete(id);
            return R.oK();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @PostMapping("pageQuery/{current}/{size}")
    public R pageQuery(
            @PathVariable Integer current,
            @PathVariable Integer size,
            @RequestBody(required = false) DictionaryTypeQuery query) {

        query.setCurrentPage(current);
        query.setPageSize(size);
        try {
            PageInfo<SystemDictionaryType> pageInfo = dictionaryType.pageQuery(query);
            return R.oK().data("rows", pageInfo.getRows()).data("totals", pageInfo.getTotals());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @PostMapping("deleteBatch")
    public R deleteBatch(@RequestBody List<Long> ids) {
        try {
            dictionaryType.deleteBatch(ids);
            return R.oK();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }
}

